package com.sskj.gocoin.mine;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.gocoin.R;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.ClassicsHeader;
import com.sskj.gocoin.common.adapter.BaseAdapter;
import com.sskj.gocoin.common.adapter.ViewHolder;
import com.sskj.gocoin.common.widget.ToolBarLayout;
import com.sskj.gocoin.m.bean.ShareEntry;
import com.sskj.gocoin.mine.data.MoneyDetail;
import com.sskj.gocoin.v.base.BaseActivity;

import java.util.Date;

import butterknife.BindView;

public class MoneyDetailActivity extends BaseActivity<MoneyDetailPresenter> {

    @BindView(R2.id.toolbar)
    ToolBarLayout toolBarLayout;
    @BindView(R2.id.mondy_detail_recyclerView)
    RecyclerView moneyDetailRecyclerView;

    @BindView(R2.id.smart_money_detail)
    SmartRefreshLayout smartRefreshLayout;

    private BaseAdapter<MoneyDetail.ListBean> adapter;

    int page = 1;

    @Override
    public void initView() {
        toolBarLayout.setLeftButtonOnClickLinster(v -> finish());
        moneyDetailRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        moneyDetailRecyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        adapter = new BaseAdapter<MoneyDetail.ListBean>(R.layout.item_money_detail, null, moneyDetailRecyclerView, true) {
            @Override
            public void bind(ViewHolder holder, MoneyDetail.ListBean item) {
                holder.setText(R.id.name, item.getNum())
                        .setText(R.id.account, item.getAccount())
                        .setText(R.id.time, item.getCreated_at());
            }
        };
        smartRefreshLayout.setRefreshHeader(new ClassicsHeader(this));
        smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                mPresenter.getData(page);
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.getData(page);
            }
        });
    }

    @Override
    public void initData() {
        mPresenter.getData(1);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, MoneyDetailActivity.class);
        context.startActivity(intent);
    }

    public void setData(MoneyDetail data) {
        if (data==null){
            return;
        }
        if (page == 1) {
            adapter.setNewData(data.getData());
        }else{
            if (data.getData()!=null)
            adapter.addData(data.getData());
        }
    }

    public void stopRefresh() {
        smartRefreshLayout.finishRefresh();
        smartRefreshLayout.finishLoadMore();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_money_detail;
    }

    @Override
    public MoneyDetailPresenter getPresenter() {
        return new MoneyDetailPresenter();
    }
}
